package com.itheima.myservlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

@WebServlet("/AddStudentServlet")
public class AddStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置代码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取表单中的数据
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String score = request.getParameter("score");

        //存储数据到本地
        BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\stu.txt", true));
        bw.write(name + "," + age + "," + score);
        bw.newLine();
        bw.close();

        //4. 增删改以后,重定向
        response.sendRedirect(request.getContextPath()+"/FindStudentServlet");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
